package com.jason.lucene.example;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;

/**
 * @author he peng
 * @create 2017/11/29 13:51
 * @see
 */
public class Searcher {

    public static void main(String[] args) throws IOException, ParseException {

        Path indexPath = Paths.get("C://lucene//index");
//        searchByFileName(indexPath , "2017");
        searchByFileContent(indexPath , "饕餮");
    }

    public static void searchByFileName(Path indexPath , String fileName) throws IOException, ParseException {
        Directory directory = FSDirectory.open(indexPath);
        DirectoryReader ireader = DirectoryReader.open(directory);
        IndexSearcher isearcher = new IndexSearcher(ireader);

        Analyzer analyzer = new StandardAnalyzer();
        QueryParser parser = new QueryParser("filename", analyzer);
        Query query = parser.parse(fileName);

        long startTime = System.currentTimeMillis();
        ScoreDoc[] hits = isearcher.search(query, 1000, Sort.INDEXORDER).scoreDocs;
        long endTime = System.currentTimeMillis();

        System.out.println("found " + hits.length + " document , cost time " +
                (endTime - startTime) + " ms");

        for (int i = 0; i < hits.length; i++) {
            Document hitDoc = isearcher.doc(hits[i].doc);
            System.err.println("filename : " + hitDoc.get("filename"));
        }

        ireader.close();
        directory.close();
    }

    public static void searchByFileContent(Path indexPath , String content) throws IOException, ParseException {
        Directory directory = FSDirectory.open(indexPath);
        DirectoryReader ireader = DirectoryReader.open(directory);
        IndexSearcher isearcher = new IndexSearcher(ireader);

        Analyzer analyzer = new StandardAnalyzer();
        QueryParser parser = new QueryParser("contents", analyzer);
        Query query = parser.parse(content);

        long startTime = System.currentTimeMillis();
        ScoreDoc[] hits = isearcher.search(query, 1000, Sort.INDEXORDER).scoreDocs;
        long endTime = System.currentTimeMillis();

        System.out.println("found " + hits.length + " document , cost time " +
                (endTime - startTime) + " ms");

        for (int i = 0; i < hits.length; i++) {
            Document hitDoc = isearcher.doc(hits[i].doc);
            System.err.println("filename : " + hitDoc.get("filename"));
        }

        ireader.close();
        directory.close();
    }
}
